<?php
require_once 'config21.php';
if (!isset($_SESSION['username'])) {
    header('Location: login21.php');
    exit();
}

if (isset($_GET['id'])) {
    $teacher_id = $_GET['id'];
    $sql = "SELECT teachers.*, departments.department_name 
            FROM teachers 
            JOIN departments ON teachers.department_id = departments.department_id 
            WHERE teacher_id = $teacher_id";
    $result = $conn->query($sql);
    if ($result->num_rows == 1) {
        $row = $result->fetch_assoc();
    } else {
        echo "未找到该教师信息";
        exit;
    }
} else {
    echo "缺少教师 ID";
    exit;
}

// 获取所有系部信息用于下拉选择
$dept_sql = "SELECT * FROM departments";
$dept_result = $conn->query($dept_sql);

// 修改教师
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST['edit_teacher'])) {
    $teacher_name = $_POST['teacher_name'];
    $gender = $_POST['gender'];
    $title = $_POST['title'];
    $contact_info = $_POST['contact_info'];
    $department_id = $_POST['department_id'];
    $age = $_POST['age'];
    $basic_salary = $_POST['basic_salary'];
    $bonus = $_POST['bonus'];
    
    // 数据验证
    if ($age < 0 || $age > 120) {
        echo "年龄必须在0-120之间";
        exit;
    }
    if ($basic_salary < 0 || $bonus < 0) {
        echo "工资和奖金不能为负数";
        exit;
    }
    
    $sql = "UPDATE teachers SET 
            teacher_name = '$teacher_name', 
            gender = '$gender', 
            title = '$title', 
            contact_info = '$contact_info', 
            department_id = $department_id,
            age = $age,
            basic_salary = $basic_salary,
            bonus = $bonus
            WHERE teacher_id = $teacher_id";
            
    if ($conn->query($sql)) {
        header('Location: teacher_management21.php');
        exit;
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
?>

<!DOCTYPE html>
<html lang="zh-hans">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改教师信息</title>
    <link rel="stylesheet" href="style.css">
    <style>
        form {
            max-width: 500px;
            margin: 20px auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="text"],
        input[type="number"],
        select {
            width: 100%;
            padding: 8px;
            margin-bottom: 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        input[type="submit"] {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
        }
        input[type="submit"]:hover {
            background-color: #0056b3;
        }
        .back-link {
            display: block;
            text-align: center;
            margin-top: 20px;
            color: #007bff;
            text-decoration: none;
        }
        .back-link:hover {
            text-decoration: underline;
        }
        .form-group {
            margin-bottom: 15px;
        }
    </style>
</head>
<body>
    <h2>修改教师信息</h2>
    <form action="<?php echo $_SERVER['PHP_SELF'] . "?id=$teacher_id" ?>" method="post">
        <div class="form-group">
            <label for="teacher_name">姓名:</label>
            <input type="text" name="teacher_name" value="<?php echo $row['teacher_name']; ?>" required>
        </div>
        
        <div class="form-group">
            <label for="gender">性别:</label>
            <select name="gender" required>
                <option value="男" <?php if($row['gender'] == '男') echo 'selected'; ?>>男</option>
                <option value="女" <?php if($row['gender'] == '女') echo 'selected'; ?>>女</option>
            </select>
        </div>
        
        <div class="form-group">
            <label for="title">职称:</label>
            <input type="text" name="title" value="<?php echo $row['title']; ?>" 
                   placeholder="例如：教授、副教授、讲师" required>
        </div>
        
        <div class="form-group">
            <label for="contact_info">联系方式:</label>
            <input type="text" name="contact_info" value="<?php echo $row['contact_info']; ?>" 
                   placeholder="电话号码">
        </div>
        
        <div class="form-group">
            <label for="department_id">所属系部:</label>
            <select name="department_id" required>
                <?php
                while ($dept_row = $dept_result->fetch_assoc()) {
                    $selected = ($dept_row['department_id'] == $row['department_id']) ? 'selected' : '';
                    echo "<option value='" . $dept_row['department_id'] . "' $selected>" . 
                         $dept_row['department_name'] . "</option>";
                }
                ?>
            </select>
        </div>
        
        <div class="form-group">
            <label for="age">年龄:</label>
            <input type="number" name="age" value="<?php echo $row['age']; ?>" 
                   min="0" max="100" required>
        </div>
        
        <div class="form-group">
            <label for="basic_salary">基本工资:</label>
            <input type="number" name="basic_salary" value="<?php echo $row['basic_salary']; ?>" 
                   min="0" step="0.01" required>
        </div>
        
        <div class="form-group">
            <label for="bonus">奖金:</label>
            <input type="number" name="bonus" value="<?php echo $row['bonus']; ?>" 
                   min="0" step="0.01" required>
        </div>
        
        <input type="submit" name="edit_teacher" value="修改">
    </form>
    <a href="teacher_management21.php" class="back-link">返回教师管理页面</a>
</body>
</html>